<template>
	<view class="aaa">
		<view class="example">
			<!-- 基础表单校验 -->
			<uni-card :is-shadow="true">
				<uni-forms ref="valiForm">
					<uni-forms-item label="手机号" name="name">
						<uni-easyinput v-model="sjData.phone" placeholder="请输入手机号" />
					</uni-forms-item>
					<uni-forms-item label="姓名" name="name">
						<uni-easyinput v-model="sjData.username" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="身份证" name="age">
						<uni-file-picker @select="select" @delete="deletel" file-mediatype="image" mode="grid"
							:limit="1" return-type="object" />

					</uni-forms-item>
					<uni-forms-item label="驾驶证" name="age">
						<uni-file-picker @select="select1" file-mediatype="image" mode="grid" :limit="1" />
					</uni-forms-item>
					<uni-forms-item label="运营资格证" name="age">
						<uni-file-picker @select="select2" file-mediatype="image" mode="grid" :limit="1" />
					</uni-forms-item>
				</uni-forms>
			</uni-card>
			<button type="primary" @click="zhuce">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/network/http.js';
	import {
		policy
	} from '@/utils/policy';
	export default {
		data() {
			return {
				code: 0,
				driver: "",
				sjData: {
					sjId:"",
					phone: "",
					username: "",
					cardId: "",
					diver: "",
					opera: "",
					invited: ""
				},
				data: {}

			}
		},
		onLoad(option) {
			request({
				url:`driver/ssmsj/info/${option.sjId}`,
				method:'GET'
			}).then(res=>{
				this.sjData.sjId=res.data.ssmSj.sjId
				this.sjData.phone=res.data.ssmSj.sjPhone
				this.sjData.username=res.data.ssmSj.sjName
				this.sjData.cardId=res.data.ssmSj.sjCardid
				this.sjData.diver=res.data.ssmSj.sjDrivie
				this.sjData.opera=res.data.ssmSj.sjOperat
			})
		},
		methods: {
			deletel(e) {
				console.log(e)
			},
			// 上传函数
			uploadFiles(tempFilePaths, i) {
				request({
					url: "admin/oss/getPolicy",
					method: "GET"
				}).then(res => {
					this.data.host = res.data.host
					this.data.signature = res.data.signature
					this.data.ossaccessKeyId = res.data.accessid
					this.data.key = res.data.dir + res.data.expire
					this.data.dir = res.data.dir
					this.data.policy = res.data.policy
					console.log(this.data.host)
					uni.uploadFile({
						url: this.data.host, //输入你的bucketname.endpoint
						filePath: tempFilePaths[0],
						fileType: 'image',
						name: 'file',
						formData: {
							key: this.data.key,
							policy: this.data.policy, // 输入你获取的的policy
							OSSAccessKeyId: this.data.ossaccessKeyId, // 输入你的AccessKeyId
							success_action_status: '200', // 让服务端返回200,不然，默认会返回204
							signature: this.data.signature // 输入你获取的的signature
						},
						success: res => {
							if (res.statusCode == 200) {
								switch (i) {
									case 1:
										this.sjData.cardId = this.data.host + "/" + this.data.key
										console.log(this.sjData.cardId)
										break
									case 2:
										this.sjData.diver = this.data.host + "/" + this.data.key
										console.log(this.sjData.diver)
										break
									case 3:
										this.sjData.opera = this.data.host + "/" + this.data.key
										console.log(this.sjData.opera)
										break
								}
							}
							//返回的是字符串，需要转成对象格式   

						},
						fail: res => {
							console.log("err");
							console.log(res);
						}

					})
				})
			},
			select(e) {
				let resc = this.uploadFiles(e.tempFilePaths, 1)

				// promises.push(promise)
			},
			select1(e) {
				let resc = this.uploadFiles(e.tempFilePaths, 2)

				// promises.push(promise)
			},
			select2(e) {
				let resc = this.uploadFiles(e.tempFilePaths, 3)

				// promises.push(promise)
			},
			zhuce() {
				console.log(this.sjData)
				request({
					url: "driver/ssmsj/update",
					method: "POST",
					data: {
						'sjData': this.sjData
					}
				}).then(res => {
					if (code == 0) {
						uni.navigateTo({
							url: `/pages/login/login?phoneNumber=${this.sjData.phone}`
						})
					}else{
						uni.showToast({
							title:"发生错误请重试",
							duration:500
						})
					}
				})

			}
		}
	}
</script>

<style>
	page {
		background-color: rgba(245, 245, 245, 1);
	}
</style>
<style scoped>
	uni-card {
		background-color: rgba(245, 245, 245, 1);
	}

	.aaa {
		background-color: rgba(245, 245, 245, 1);
	}
</style>
